package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.BaseDict;
import com.itheima.domain.Customer;
import com.itheima.domain.LinkMan;
import com.itheima.domain.UserInfo;
import com.itheima.service.BaseDictService;
import com.itheima.service.CustomerService;
import com.itheima.service.LinkManService;
import com.itheima.service.UserInfoService;
import com.sun.xml.internal.ws.wsdl.parser.MemberSubmissionAddressingWSDLParserExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * ClassName:CustomerController
 * Package:com.itheima.controller
 * Description:
 * Date:2020/3/16 23:48
 * User: snow
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {
	@Autowired
	private CustomerService customerService;
	@Autowired
	private BaseDictService baseDictService;
	@Autowired
	private LinkManService linkManService;
	@Autowired
	private UserInfoService userInfoService;
	// 客户来源
	@Value("${customer.source.type}")
	private String SOURCE_TYPE;
	// 客户所属行业
	@Value("${customer.industry.type}")
	private String INDUSTRY_TYPE;
	// 客户级别
	@Value("${customer.level.type}")
	private String LEVEL_TYPE;


	/**
	 * 查找所有客户信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findAll.do")
	public ModelAndView findAll
	(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page,
	 @RequestParam(name = "size", required = true,defaultValue = "14")Integer size) throws Exception {
		ModelAndView view = new ModelAndView();
		List<Customer> customers = customerService.findAll(page,size);
		// 客户来源
		List<BaseDict> sourceType = baseDictService
				.findBaseDictByTypeCode(SOURCE_TYPE);
		// 客户所属行业
		List<BaseDict> industryType = baseDictService
				.findBaseDictByTypeCode(INDUSTRY_TYPE);
		// 客户级别
		List<BaseDict> levelType = baseDictService
				.findBaseDictByTypeCode(LEVEL_TYPE);
		view.addObject("sourceType", sourceType);
		view.addObject("industryType", industryType);
		view.addObject("levelType", levelType);
		// PageInfo是一个分页bean
		PageInfo pageInfo = new PageInfo(customers);
		view.addObject("pageInfo",pageInfo);
		// view.addObject("customerList", customers);
		view.setViewName("customer/customer-list");
		return view;
	}

	/**
	 * 模糊查询
	 * @param customerName
	 * @param customerLevel
	 * @param customerSource
	 * @param customerIndustry
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findSome.do")
	public ModelAndView findSome
	(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page,
	 @RequestParam(name = "size", required = true,defaultValue = "14")Integer size,
	 String customerName,String customerLevel, String customerSource,String customerIndustry) throws Exception {
		ModelAndView view = new ModelAndView();
		List<Customer> customers = customerService.findSome(customerName,customerLevel
				,customerSource,customerIndustry,page,size);
		// 客户来源
		List<BaseDict> sourceType = baseDictService
				.findBaseDictByTypeCode(SOURCE_TYPE);
		// 客户所属行业
		List<BaseDict> industryType = baseDictService
				.findBaseDictByTypeCode(INDUSTRY_TYPE);
		// 客户级别
		List<BaseDict> levelType = baseDictService
				.findBaseDictByTypeCode(LEVEL_TYPE);
		view.addObject("sourceType", sourceType);
		view.addObject("industryType", industryType);
		view.addObject("levelType", levelType);
		// 查询的数据返回到结果中
		view.addObject("customerIndustry",customerIndustry);
		view.addObject("customerLevel",customerLevel);
		view.addObject("customerSource",customerSource);
		// PageInfo是一个分页bean
		PageInfo pageInfo = new PageInfo(customers);
		view.addObject("pageInfo",pageInfo);
		view.setViewName("customer/customer-list");
		return view;
	}


	/**
	 * 查找客户详细信息
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findById.do")
	public ModelAndView findById(String id) throws Exception {
		ModelAndView view = new ModelAndView();
		Customer customer = customerService.findById(id);
		view.addObject("customer", customer);
		List<LinkMan> linkManList = linkManService.findLinkManByCustomerId(id);
		view.addObject("linkManList",linkManList);
		view.setViewName("customer/customer-show");
		return view;

	}


	/**
	 * 创建客户前加载基本信息
	 */
	@RequestMapping("/saveBefore.do")
	public ModelAndView saveBefore() throws Exception {
		ModelAndView view = new ModelAndView();
		// 客户来源
		List<BaseDict> sourceType = baseDictService
				.findBaseDictByTypeCode(SOURCE_TYPE);
		// 客户所属行业
		List<BaseDict> industryType = baseDictService
				.findBaseDictByTypeCode(INDUSTRY_TYPE);
		// 客户级别
		List<BaseDict> levelType = baseDictService
				.findBaseDictByTypeCode(LEVEL_TYPE);
		view.addObject("sourceType", sourceType);
		view.addObject("industryType", industryType);
		view.addObject("levelType", levelType);
		List<UserInfo> userInfoList = userInfoService.findAll();
		view.addObject("userInfoList",userInfoList);
		view.setViewName("customer/customer-add");
		return view;
	}


	/**
	 * 创建客户
	 */
	@RequestMapping("/save.do")
	public String save(Customer customer,String userName) throws Exception {
		// 将当前用户id存储在客户对象中
		UserInfo creatInfo = userInfoService.findByName(userName);
		//获取当前用户id，送给creatInfo.id,传给creatId
		System.out.println(creatInfo.getId());
		customer.setCreatInfo(creatInfo);
		// 创建Date对象
		Date date = new Date();
		// 得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
		Timestamp timeStamp = new Timestamp(date.getTime());
		customer.setCreatTime(timeStamp);
		// 执行Service层中的创建方法，返回的是受影响的行数
		customerService.save(customer);
		return "redirect:findAll.do";
	}


	/**
	 * 删除客户
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete.do")
	public String delete( String id) throws Exception {
		customerService.delete(id);
	  return "redirect:findAll.do";
	}

	/**
	 * 更改客户前查找详细信息
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateBefore.do")
	public ModelAndView updateBefore(String id) throws Exception {
		ModelAndView view = new ModelAndView();
		Customer customer = customerService.findById(id);
		// 客户来源
		List<BaseDict> sourceType = baseDictService
				.findBaseDictByTypeCode(SOURCE_TYPE);
		// 客户所属行业
		List<BaseDict> industryType = baseDictService
				.findBaseDictByTypeCode(INDUSTRY_TYPE);
		// 客户级别
		List<BaseDict> levelType = baseDictService
				.findBaseDictByTypeCode(LEVEL_TYPE);
		view.addObject("sourceType", sourceType);
		view.addObject("industryType", industryType);
		view.addObject("levelType", levelType);
		view.addObject("customer",customer);
		view.setViewName("customer/customer-update");
		return view;
	}

	/**
	 * 更改客户
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/update.do")
	public String update(Customer customer) throws Exception {
		customerService.update(customer);
		return "redirect:findAll.do";
	}

}
